package com.jingan.nursinghome.regulatory;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.dialog.CreateMicrometerTableDailog;
import com.jingan.nursinghome.regulatory.dialog.LoginDialog;
import com.jingan.nursinghome.regulatory.dialog2.CreateMicrometerTableDailog2;
import com.jingan.nursinghome.regulatory.dialog3.CreateMicrometerTableDailog3;
import com.jingan.nursinghome.regulatory.dialog3.MicrometerTableOSDialog3;
import com.jingan.nursinghome.regulatory.http.Http;
import com.jingan.nursinghome.regulatory.view.TabView;


import java.io.File;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.base.BasePhotoActivity;
import cn.android.support.v7.lib.sin.crown.dialog.PhotoDialog;
import cn.android.support.v7.lib.sin.crown.dialog.ProgressDailog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;


public class MainActivity extends BasePhotoActivity {
    public static MainActivity mainActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivity = this;
        BaseApplication.getInstance().setStatusBarDrak(getWindow(), true);
        setContentView(R.layout.activity_main);
    }

    TabView tab1, tab2, tab3;

    @Override
    protected void initUI() {
        tab1 = (TabView) findViewById(R.id.tab1);
        tab1.setCategory(0);
        tab2 = (TabView) findViewById(R.id.tab2);
        tab2.setCategory(1);
        tab3 = (TabView) findViewById(R.id.tab3);
        tab3.setCategory(2);
    }

    @Override
    protected void adapterUI() {
        ProportionUtils.getInstance().adapterOutView(findViewById(R.id.activity_main_constraintLayout), false);
        ProportionUtils.getInstance().adapterTextView((TextView) findViewById(R.id.txt_title));
        ProportionUtils.getInstance().adapterTextView((TextView) findViewById(R.id.txt_login_out));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.view_top));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.view_top_line));
    }

    @Override
    protected void listener() {

        //初始化数据
        MicrometerTable.getInstance().getTopicsAll();//千分表所有数据
        MicrometerTable.getInstance().getJlys();//获取敬老院数据
        MicrometerTable.getInstance().getLinkups();//获取主要沟通人员
        MicrometerTable.getInstance().getTimuTypes();//获取题目类型

        //登录
        loginOrOut(true);
        //退出登录
        findViewById(R.id.txt_login_out).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginOrOut(false);
            }
        });
        tab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //千分表
                CreateMicrometerTableDailog.getInstance(MainActivity.this);
            }
        });
        tab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //日常表
                CreateMicrometerTableDailog2.getInstance(MainActivity.this);
            }
        });
        tab3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //专项表
                CreateMicrometerTableDailog3.getInstance(MainActivity.this);
            }
        });

    }

    //true登录，false退出登录
    private void loginOrOut(boolean login) {
        LoginDialog.getInstance(MainActivity.this, login, new BaseListenner() {
            @Override
            public void onResult(Object result) {
                //MicrometerTable.getInstance().getTopics();//登录成之后，初始化数据

                if (BaseApplication.getInstance().isFirstOpen(MainActivity.this)) {
                    //首次进入程序(加载数据三秒)
                    mainActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressDailog = ProgressDailog.getInstance(mainActivity);
                            tab1.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    progressDailog.dismiss();
                                }
                            }, 3500);
                        }
                    });
                }

            }
        });
    }

    ProgressDailog progressDailog;

    @Override
    protected void onfinish() {
        //数据保存
        MicrometerTable.getInstance().saveTopics();//千分表
        MicrometerTable.getInstance().saveRichangIndex();//日常表
        MicrometerTable.getInstance().saveTOpicsZX();//专项表
    }

    @Override
    public void onCropResult(File file, Bitmap bitmap) {

    }

    @Override
    public void onSuccess(File file, Bitmap bitmap) {
        if (PhotoDialog.mBasePhotoListenner != null) {
            PhotoDialog.mBasePhotoListenner.onSuccess(file, bitmap);
        }
    }

    @Override
    public void onFailure(String result) {

    }

    @Override
    public void onRequestPermissionsSuccess() {
        camera();
    }
}
